using System;

namespace Nsb
{
    //[IgnoreNamespace] Dont use because called from javascript
    public static class Encoder
    {
        public static string LzwEncode(string s)
        {
            return (string)Script.Literal("Encoder.LzwEncode({0})", s);
        }

        public static string LzwDecode(string s)
        {
            return (string)Script.Literal("Encoder.LzwDecode({0})", s);
        }

        public static string Encode64(string s)
        {
            return (string)Script.Literal("Encoder.Encode64({0})", s);
        }

        public static string Decode64(string s)
        {
            return (string)Script.Literal("Encoder.Decode64({0})", s);
        }
/*
        private const string KeyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        public static string EncodeX64(string input)
        {
            string ot = "";
            int i = 0;

            input = Utf8Encode(input);

            while (i < input.Length)
            {
                int chr1, chr2, chr3, enc1, enc2, enc3, enc4;
                chr1 = input.CharCodeAt(i++);
                chr2 = input.CharCodeAt(i++);
                chr3 = input.CharCodeAt(i++);

                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;

                if ((bool) Script.Literal("isNaN({0})", chr2))
                {
                    enc3 = enc4 = 64;
                }
                else if ((bool) Script.Literal("isNaN({0})", chr3))
                {
                    enc4 = 64;
                }

                ot += KeyStr.CharAt(enc1) + KeyStr.CharAt(enc2) + KeyStr.CharAt(enc3) + KeyStr.CharAt(enc4);
            }
            return ot;
        }

        private static string Utf8Encode(string input)
        {
            input = input.ReplaceRegex((RegularExpression) Script.Literal("/\\r\\n/g"), "\n");
            string utftext = "";

            for (int n = 0; n < input.Length; n++)
            {
                int c = input.CharCodeAt(n);

                if (c < 128)
                {
                    utftext += String.FromCharCode(c);
                }
                else if ((c > 127) && (c < 2048))
                {
                    utftext += String.FromCharCode((c >> 6) | 192);
                    utftext += String.FromCharCode((c & 63) | 128);
                }
                else
                {
                    utftext += String.FromCharCode((c >> 12) | 224);
                    utftext += String.FromCharCode(((c >> 6) & 63) | 128);
                    utftext += String.FromCharCode((c & 63) | 128);
                }
            }

            return utftext;
        }
*/
    }
}